package com.qcby.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author haozeWang
 * @create 2021-09-14 14:27
 */
@Controller
@RequestMapping("/test")
public class TestController {
        @RequestMapping("/test1")
        public void demo(
                //默认装配，可传可不传
                Model model,String name,
                @RequestParam String name0,
                /*必传参数，通过value定义参数名，不传报错*/
                @RequestParam(value = "name1") String name1
                //通过name属性定义名称和是否必传，非必传
                ,@RequestParam(name = "name2",required = false) String name2
                //通过value属性定义名称，必传，但有默认参数，也可不传，将自动赋值
                ,@RequestParam(name = "name3",defaultValue = "t3") String name3
                //不必要加name或value的自定义参数名，必传
                ,@RequestParam("name444") String name4

        ){
            System.out.println("name:"+name);
            System.out.println("name0:"+name0);
            System.out.println("name1:"+name1);
            System.out.println("name2:"+name2);
            System.out.println("name3:"+name3);
            System.out.println("name4:"+name4);

        }
        @RequestMapping(value = {"/demo1/{name1}/{name2}/{name3}",
                //name3选传
                "demo1/{name1}/{name2}"})
        public void demo2(Model model,String name,
           //必传，通过请求url来传
            @PathVariable String name1,
             //必传
             @PathVariable("name2") String name2,
              //选传
              @PathVariable(value = "name3",required = false) String name4
        ){
            System.out.println("name:"+name);
            System.out.println("name1:"+name1);
            System.out.println("name2:"+name2);
            System.out.println("name3:"+name4);

        }
}
